﻿Public Class frm_mediodepago

    Dim conexion_bd As String
    Enum estado
        insertar
        modificar
    End Enum
    Enum termino
        aprobado
        rechazado
    End Enum
    Dim accion As estado = estado.insertar

    Private Sub frm_mediodepago_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'conexión a la base de datos'
        Me.conexion_bd = "Provider=SQLOLEDB;Data Source=GTºN1\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=C:\DB\LIBRERIA_MAGOO.MDF"

        'carga la lista
        carga_lista(Me.leer_tabla())

        Me.btn_nuevo.Focus()
        Me.btn_quitar.Enabled = False

    End Sub

    Private Sub btn_quitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_quitar.Click
        If MessageBox.Show("¿Esta seguro que desea borrar el cliente seleccionado?", "Importante", _
                                            MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
            Me.quitar()
        End If
    End Sub

    Private Sub btn_guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_guardar.Click
        If Me.validar() = True Then
            If Me.accion = estado.insertar Then
                If Me.validar_existencia() = termino.aprobado Then 'valida si existe otro igual
                    Me.nuevo()  'falta validar si existe otro igual'
                    MessageBox.Show("Medio de Pago agregado", "Importante", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MsgBox("El medio de pago que quiere agregar ya existe", MsgBoxStyle.Critical, "Importante")
                    Exit Sub
                End If
            Else
                Me.modificar()
                MessageBox.Show("Medio de Pago modificado", "Importante", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            carga_lista(Me.leer_tabla())

            Me.txt_descripcion.Text = ""
            Me.btn_nuevo.Focus()
        End If
        Me.txt_descripcion.Enabled = False


    End Sub
    Private Sub btn_nuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nuevo.Click
        Me.txt_descripcion.Enabled = True
        Me.txt_descripcion.Text = ""
        Me.txt_descripcion.Focus()
        Me.btn_quitar.Enabled = False
        Me.accion = estado.insertar
    End Sub

    Private Function nuevo() As termino
        Dim conexion As New Data.OleDb.OleDbConnection
        Dim command As New Data.OleDb.OleDbCommand
        Dim consulta As String = "" 'declaro la variable consulta, vacia'


        'abro conexion'
        conexion.ConnectionString = Me.conexion_bd
        conexion.Open()

        'consulta de nuevo'
        consulta = "insert into MedioPago(descripcion) values('" & Me.txt_descripcion.Text & "')"

        command.CommandType = CommandType.Text
        command.CommandText = consulta
        command.Connection = conexion
        command.ExecuteNonQuery()
        conexion.Close()
        Return termino.aprobado
    End Function
    Private Function modificar() As termino
        Dim conexion As New Data.OleDb.OleDbConnection
        Dim command As New Data.OleDb.OleDbCommand
        Dim consulta As String = "" 'declaro la variable consulta, vacia'

        'abro conexion'
        conexion.ConnectionString = Me.conexion_bd
        conexion.Open()

        'consulta de nuevo'
        consulta = "Update MedioPago Set descripcion='" & Me.txt_descripcion.Text & "' where id_pago='" & Me.lst_mediopago.SelectedValue & "'"

        command.CommandType = CommandType.Text
        command.CommandText = consulta
        command.Connection = conexion
        command.ExecuteNonQuery()
        conexion.Close()
        Return termino.aprobado

    End Function
    Private Function quitar()

        Dim conexion As New Data.OleDb.OleDbConnection
        Dim command As New Data.OleDb.OleDbCommand

        'abro conexion'
        conexion.ConnectionString = Me.conexion_bd
        conexion.Open()

        command.Connection = conexion
        command.CommandType = CommandType.Text
        command.CommandText = "delete from MedioPago where descripcion= '" & Me.txt_descripcion.Text & "'"
        command.ExecuteNonQuery()
        conexion.Close()

        carga_lista(Me.leer_tabla())
        MessageBox.Show("Medio de Pago quitado", "Importante", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.txt_descripcion.Text = ""
        Me.btn_quitar.Enabled = False
        Return termino.aprobado
    End Function
    Private Function leer_tabla() As Data.DataTable
        Dim conexion As New Data.OleDb.OleDbConnection
        Dim command As New Data.OleDb.OleDbCommand
        Dim tabla As New Data.DataTable 'declaro la variable tabla'

        'abro conexion'
        conexion.ConnectionString = Me.conexion_bd
        conexion.Open()

        command.Connection = conexion
        command.CommandType = CommandType.Text
        command.CommandText = "select * from MedioPago"

        tabla.Load(command.ExecuteReader())
        conexion.Close()
        Return tabla
    End Function

    Private Sub carga_lista(ByVal tabla As Data.DataTable)
        Me.lst_mediopago.DataSource = tabla
        Me.lst_mediopago.ValueMember = "id_pago"
        Me.lst_mediopago.DisplayMember = "descripcion"
    End Sub

    Private Sub lst_mediopago_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lst_mediopago.Click
        Dim desc As String = sender.displaymember

        Dim conexion As New Data.OleDb.OleDbConnection
        Dim command As New Data.OleDb.OleDbCommand
        Dim tabla As New Data.DataTable

        conexion.ConnectionString = Me.conexion_bd
        conexion.Open()

        command.Connection = conexion
        command.CommandType = CommandType.Text
        command.CommandText = " select * from MedioPago where descripcion=" & desc

        tabla.Load(command.ExecuteReader())

        Me.txt_descripcion.Text = Me.lst_mediopago.Text
        Me.txt_descripcion.Enabled = True
        Me.txt_descripcion.Focus()

        Me.accion = estado.modificar
        conexion.Close()

        Me.btn_quitar.Enabled = True
    End Sub

    Private Function validar() As Boolean
        If Me.txt_descripcion.Text = "" Then
            MsgBox("La denominación no puede esta vacia", MsgBoxStyle.Critical, "Importante")
            Me.txt_descripcion.Focus()
            Return False
        End If

        Return True

    End Function
    Private Function validar_existencia() As termino
        Dim conexion As New Data.OleDb.OleDbConnection
        Dim command As New Data.OleDb.OleDbCommand
        Dim tabla As New Data.DataTable
        Dim consulta As String = ""

        conexion.ConnectionString = Me.conexion_bd
        conexion.Open()

        consulta = "select * from MedioPago where descripcion = '" & Me.txt_descripcion.Text & "'"

        command.CommandType = CommandType.Text
        command.CommandText = consulta
        command.Connection = conexion

        tabla.Load(command.ExecuteReader())

        conexion.Close()

        If tabla.Rows.Count() = 1 Then
            Return termino.rechazado
        Else
            Return termino.aprobado
        End If


    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If System.Windows.Forms.DialogResult.OK = MessageBox.Show("Cerrar esta ventana?", "Importante", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) Then
            Me.Close()
        End If
    End Sub
End Class